﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace InstaMe.Source.Model
{
    [Table(Name = "DiaryItem")]
    public class DiaryItem : INotifyPropertyChanged
    {
        // base
        private long mId;
        private string mText;
        private DateTime? mTime;
        private byte mType;

        // photo
        private string mPhoto;

        // video
        private string mVideo;

        // audio
        private string mAudio;

        public const byte TYPE_UNKNOW = 0;
        public const byte TYPE_TEXT = 1;
        public const byte TYPE_PHOTO = 2;
        public const byte TYPE_AUDIO = 3;
        public const byte TYPE_VIDEO = 4;

        public DiaryItem()
        {
            mId = 0;
            mText = "";
            mTime = null;
            mType = TYPE_UNKNOW;
            mPhoto = "";
            mVideo = "";
            mAudio = "";
        }

        public DiaryItem(long aId, DateTime aTime, string aText, string aPhoto, string aVideo, string aAudio, byte aType)
        {
            mId = aId;
            mText = aText;
            mTime = aTime;
            mType = aType;
            mPhoto = aPhoto;
            mVideo = aVideo;
            mAudio = aAudio;
        }

        public long getId()
        {
            return mId;
        }

        public void setText(string aText)
        {
            mText = aText;
            NotifyPropertyChanged("Model_Text");
        }

        public String getText()
        {
            return mText;
        }

        public void setTime(DateTime aTime)
        {
            mTime = aTime;
            NotifyPropertyChanged("Model_Time");
        }

        public DateTime? getTime()
        {
            return mTime;
        }

        public void setType(byte aType)
        {
            mType = aType;
            NotifyPropertyChanged("Model_Type");
        }

        public byte getType()
        {
            return mType;
        }

        public void setPhoto(string aPhoto)
        {
            mPhoto = aPhoto;
            NotifyPropertyChanged("Model_Photo");
        }

        public string getPhoto()
        {
            return mPhoto;
        }

        public void setAudio(string aAudio)
        {
            mAudio = aAudio;
            NotifyPropertyChanged("Model_Audio");
        }

        public string getAudio()
        {
            return mAudio;
        }

        public void setVieo(string aVideo)
        {
            mVideo = aVideo;
            NotifyPropertyChanged("Model_Video");
        }

        public string getVideo()
        {
            return mVideo;
        }
        //============================================================================
        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChangedOutDispatcher(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        protected void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                });
            }
        }

        //=================================== Data Binding =========================================
        public string Model_Text
        {
            get
            {
                return mText;
            }
        }

        public DateTime? Model_Time
        {
            get
            {
                return mTime;
            }
        }

        public byte Model_Type
        {
            get
            {
                return mType;
            }
        }

        public string Model_Photo
        {
            get
            {
                return mPhoto;
            }
        }

        public string Model_Audio
        {
            get
            {
                return mAudio;
            }
        }

        public string Model_Video
        {
            get
            {
                return mVideo;
            }
        }
        /************************************************************************/
        /*       Table                                                               */
        /************************************************************************/
        [Column(DbType = "INT NOT NULL IDENTITY", IsDbGenerated = true,
            IsPrimaryKey = true, AutoSync = AutoSync.OnInsert)]
        public long id 
        {
            get { return mId; }
            set { mId = value; }
        }

        [Column(IsVersion = true)]
        private Binary _version;

        [Column(Name = "text")]
        public string db_text
        {
            get { return mText; }
            set { mText = value; }
        }

        [Column(Name = "datetime")]
        public DateTime? db_datetime
        {
            get { return mTime; }
            set { mTime = value; }
        }

        [Column(Name = "type")]
        public byte db_type
        {
            get { return mType; }
            set { mType = value; }
        }

        [Column(Name = "photo")]
        public string db_photo
        {
            get { return mPhoto; }
            set { mPhoto = value; }
        }

        [Column(Name = "audio")]
        public string db_audio
        {
            get { return mAudio; }
            set { mAudio = value; }
        }

        [Column(Name = "video")]
        public string db_video
        {
            get { return mVideo; }
            set { mVideo = value; }
        }

        public void MappingData(DiaryItem aOther)
        {
            db_text = aOther.db_text;
            db_type = aOther.db_type;
            db_datetime = aOther.db_datetime;
            db_photo = aOther.db_photo;
            db_audio = aOther.db_audio;
            db_video = aOther.db_video;
        }
    }
}
